<?php

namespace App\Http\ControllersVer1;

use Illuminate\Http\Request;

use Namshi\JOSE\SimpleJWS;
//use Requests;
use Carbon\Carbon;
use Webpatser\Uuid\Uuid;

use App\User;

class AuthController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }


    /**
     * @param $user
     * @return string
     */
    private function getToken($user)
    {
        //
        $jws = new SimpleJWS(array(
            'alg' => 'HS256'
        ));

        $jws->setPayload(array(
            'uid' => $user->id,
            'exp' => Carbon::now()->addDay(30)->getTimestamp(),
            //Carbon::now()->addMinutes(3)->getTimestamp(),
        ));

        $secretKey = env('JWT_KEY');

        $jws->sign($secretKey);
        $token = $jws->getTokenString();

        return $token;

    }





    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function loginWithSms(Request $request) {

        $this->validate($request, [
            'mobile' => 'required|digits:11',
            'sms_code' => 'required|digits:6',
            'longitude' => 'numeric|between:-180,180',
            'latitude' => 'numeric|between:-85.05112878,85.05112878'
        ]);

        //
        $mobile = $request->input('mobile');
        $prefix_code = "86";
        $sms_code = $request->input('sms_code');

//        //sms_code
//        if ($prefix_code == '86' && $mobile == '99912345678') {
//            //for ios app apple audit
//            $code_saved = Redis::get('sms_code_' . $prefix_code . $mobile);
//
//        } elseif ($prefix_code == '86' && substr($mobile, 0, 3) == '999') {
//            //alex's mobile
//            $code_saved = Redis::get('sms_code_86999');
//
//        } elseif ($prefix_code == '86' && substr($mobile, 0, 3) == '899') {
//            //alex's mobile
//            $code_saved = Redis::get('sms_code_86899');
//
//        } elseif ($prefix_code == '86' && substr($mobile, 0, 3) == '799') {
//            //alex's mobile
//            $code_saved = Redis::get('sms_code_86799');
//
//        } elseif ($prefix_code == '86' && substr($mobile, 0, 3) == '699') {
//            //php's admin mobile
//            $code_saved = Redis::get('sms_code_86699');
//
//        } else {
//
//            $code_saved = Redis::get('sms_code_' . $prefix_code . $mobile);
//
//        }

        $code_saved = "123456";

        if ($sms_code != $code_saved) {

            return response()->json([
                'code' => 40600,
                'msg' => 'wrong sms code.'
            ]);

        }

        //
        $user = User::where([
            ['mobile', $mobile]
        ])->first();


        if ($user == null) {

            //register new user
            $user = new User;
            $user->uuid = str_replace('-', '', Uuid::generate(4)->string);
            $user->mobile = $mobile;
            $user->save();
            //get all properties for User Model.
            $user = User::find($user->id);

            $code = 2000;
            $msg = "registered new user.";

            //Event::fire(new RegisterUserEvent($user));
        } else {

            $code = 2000;
            $msg = "login success";

        }

        $token = $this->getToken($user);

        //del the sms code
//        Redis::del('sms_code_' . $prefix_code . $mobile);

//        //
//        $login_with = 'sms';
//        event(new LoginUserEvent(
//            $user,
//            $login_with,
//            $request->header('DeviceToken'),
//            $request->header('DeviceUUID'),
//            $request->header('DeviceVersion'),
//            $request->header('DeviceOS'),
//            $request->header('OSVersion'),
//            $request->header('AppVersion'),
//            $request->header('ip_address'),
//            $request->input('longitude'),
//            $request->input('latitude')
//        ));

        return response()->json([
            'code' => $code,
            'msg' => $msg,
            'me' => $user,
            'mobile' => $user->mobile,
            'api_token' => $token
        ]);

    }

}
